const express = require("express");
const router = require("./router");
const cors = require("cors");

const fs = require("fs"); // 文件处理
const https = require("https"); //处理https
const privateKey = fs.readFileSync("https/2_apitest.leoit.top.key", "utf8"); //密钥
const certificate = fs.readFileSync(
  "https/1_apitest.leoit.top_bundle.crt",
  "utf8"
); // 证书
const credentials = { key: privateKey, cert: certificate };

const bodyParser = require("body-parser");

// 创建 express 应用
const app = express();
app.use(cors());
// 使用body-parser 解析参数，需要放在 app.use("/", router); 之前
// app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.json());
//handle request entity too large
app.use(bodyParser.json({ limit: "10mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use("/", router); // 添加路由中间件

// 使 express 监听 5000 端口号发起的 http 请求
const server = app.listen(5000, function () {
  const { address, port } = server.address();
  console.log("Http Server is running on http://%s:%s", address, port);
});

// 创建https服务
const httpsServer = https.createServer(credentials, app); // 创建https服务
const SSLPORT = 18082; //端口
// 设置 监听 https 请求
httpsServer.listen(SSLPORT, function () {
  console.log("HTTPS server is running on: https://localhost:%s", SSLPORT);
});
